/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-1-16         admin
 */

package com.coocaa.app.gamecenter.pages.home;

import android.util.Xml;

import com.tianci.appstore.ui.widget.SkyCustomLayout.BlockData;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 * @ClassName ReadXMLConfig
 * @date 2014-1-16
 */
public class ReadXMLParser {

    public static CategoryData parserXML(InputStream is) throws Exception {
        CategoryData data = new CategoryData();
        List<BlockData> blockDatas = new ArrayList<BlockData>();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "UTF-8");
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:

                    break;
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("category")) {
                        data.mRow = Integer.valueOf(parser.getAttributeValue(null, "row"));
                        data.mColumn = Integer.valueOf(parser.getAttributeValue(null, "column"));
                        data.mWidth = Integer.valueOf(parser.getAttributeValue(null, "width"));
                        data.mHeight = Integer.valueOf(parser.getAttributeValue(null, "height"));
                    } else if (parser.getName().equals("item")) {
                        BlockData blockData = new BlockData();
                        int[] block = generatePos(parser.getAttributeValue(null, "shape"));
                        blockData.mBlock = block;
                        int len = block.length;
                        blockData.mColNum = 1;
                        blockData.mRowNum = 1;
                        if (len > 1) {
                            if (len % 2 != 0) {
                                blockData.mColNum = len;
                            } else {
                                for (int i = 1; i < len; i++) {
                                    boolean column = true;
                                    if (block[i] - block[i - 1] < data.mColumn - 1 && column) {
                                        blockData.mColNum++;
                                    } else {
                                        blockData.mRowNum++;
                                        column = false;
                                    }
                                }
                            }
                        }
                        if (block[0] < data.mColumn) {
                            blockData.mTurnPage = new boolean[]{false, true, false, false};
                        } else if (block[0] > data.mColumn * (data.mRow - 1)) {
                            blockData.mTurnPage = new boolean[]{false, false, false, true};
                        } else {
                            blockData.mTurnPage = new boolean[]{false, false, false, false};
                        }
                        blockDatas.add(blockData);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    break;
            }
            eventType = parser.next();
        }
        data.mLayoutData = blockDatas;
        return data;
    }

    public static int[] generatePos(String pos) {
        int[] intPos = null;
        if (pos != null && !pos.equals("")) {
            String[] poss = pos.split(",");
            int len = poss.length;
            intPos = new int[len];
            for (int i = 0; i < len; i++) {
                intPos[i] = Integer.valueOf(poss[i]);
            }
        }
        return intPos;
    }

}
